﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace sinetd.Utilities
{
    public static class ConsoleAddons
    {
        public static string PrintTable(this List<string[]> list)
        {
            StringBuilder sb = new StringBuilder();
            if (list.Count > 0)
            {
                int count = list[0].Length;
                int[] llist = new int[count];
                int sum = 0;
                for (int i = 0; i < count; i++)
                {
                    llist[i] = list.Max(t => t[i].Length);
                    sum += llist[i] + 3;
                }
                bool header = true;

                sb.Append(new String('-', sum) + "\n");
                foreach (string[] item in list)
                {
                    for (int i = 0; i < count; i++)
                    {
                        sb.Append(String.Format(" {0,-" + llist[i] + "}", item[i]));
                        if (i + 1 < count) sb.Append(" |");
                    }
                    sb.Append("\n");
                    if (header)
                    {
                        sb.Append(new String('-', sum) + "\n");
                        header = false;
                    }
                }
                if (list.Count > 1)
                    sb.Append(new String('-', sum) + "\n");
            }

            return sb.ToString();
        }

        public static string PrintDictionary(this Dictionary<string, string> dict)
        {
            List<string[]> list = new List<string[]>();
            list.Add(dict.Keys.ToArray());
            list.Add(dict.Values.ToArray());
            return list.PrintTable();
        }

        public static string[] ToHexArray(this byte[] bytes, int split = 1, int offset = 0)
        {
            string[] output = new string[(bytes.Length - offset) / split + (bytes.Length - offset) % split];
            for (int i = 0; i < output.Length; i++)
            {
                for (int j = 0; j < split; j++)
                    output[i] += bytes[offset + i * split + j].ToString("X2");
            }
            return output;
        }

        public static void ParseArguments(string[] args, Dictionary<string, string> cfg, string[] default_prefix)
        {
            int count = args.Length;
            int i = 0;
            int std_param = 0;
            while (i < count)
            {
                if (args[i][0] == '-')
                {
                    cfg[args[i].Substring(1)] = args[++i];
                }
                else
                {
                    string opt_name = null;
                    if (default_prefix != null)
                        if (std_param < default_prefix.Length)
                            opt_name = default_prefix[std_param];
                    if (opt_name == null) opt_name = "param" + std_param;
                    cfg[opt_name] = args[i];
                    std_param++;
                }
                i++;
            }
        }

        public static void ParseArguments(string[] args, Dictionary<string, string> cfg)
        {
            ParseArguments(args, cfg, null);
        }
    }
}
